package com.atguigu.param;

import com.atguigu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("param")
public class ParamController {
    //直接接收数据
    // param/test?name=zhangsan&age=18
    @RequestMapping("test")
    @ResponseBody // 返回数据
    public String test(String name, Integer age)
    {
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        return "success";
    }

    // 注解指定
    // 指定任意的请求参数 要求必须传递 要求不必须穿钉 给与一个默认值

    /**
     * @RequestParam-> 指定请求参数 或者是否必须传递 或者非必须传递设置默认值
     *              (value = "指定请求参数名，如果形参名和请求参数名一直 可以省略",
     *              required = false 前端是否必须传递此参数 默认是必须 不传400异常 ,
     *              defaultValue = "zhangsan" 设置默认值)
     *
     */
    @RequestMapping("test2")
    @ResponseBody // 返回数据
    public String test2(@RequestParam(value = "username", defaultValue = "zhangsan") String name,
                        @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                        Integer pageSize)
    {
        System.out.println("name = " + name);
        System.out.println("pageNo = " + pageNo);
        System.out.println("pageSize = " + pageSize);

        return "success";
    }

    //特殊值
    //一名多值
    @RequestMapping("test3")
    @ResponseBody
    public String test3(@RequestParam List<String> ids)
    {
        for (String s : ids) {
            System.out.println("ids = " + s);
        }
        return "success";
    }

    //使用实体类对象封装参数
    @RequestMapping("test4")
    @ResponseBody
    public String test4(User user)
    {
        System.out.println("user = " + user);
        return "success";
    }


}
